package com.varian.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.varian.log.annotation.ApiLog;
import com.varian.service.annotation.Resubmit;
import com.varian.service.web.BaseController;
import com.varian.system.dto.*;
import com.varian.system.service.ISysUserService;
import com.varian.system.vo.SysUserGrantVO;
import com.varian.system.vo.SysUserProfileVO;
import com.varian.system.vo.SysUserVO;
import com.varian.tool.result.IR;
import com.varian.tool.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ben
 * @since 2024/7/1
 */
@Resubmit
@RestController
@Tag(name = "用户控制层")
@RequestMapping("/user")
public class SysUserController extends BaseController<ISysUserService> {

    @ApiLog
    @PostMapping
    @Operation(summary = "新增用户")
    @PreAuthorize("hasAuthority('user:add')")
    public IR add(@Validated @RequestBody SysUserDTO dto) {
        return R.status(baseService.add(dto));
    }

    @ApiLog
    @PutMapping("{id:\\d+}")
    @Operation(summary = "修改用户")
    @PreAuthorize("hasAuthority('user:modify')")
    public IR modify(@PathVariable("id") Long id, @Validated @RequestBody SysUserDTO dto) {
        return R.status(baseService.modify(id, dto));
    }

    @ApiLog
    @GetMapping("{id:\\d+}")
    @Operation(summary = "用户详情")
    @PreAuthorize("hasAuthority('user:detail')")
    public R<SysUserVO> detail(@PathVariable("id") Long id) {
        return R.data(baseService.detail(id));
    }

    @ApiLog
    @GetMapping("page")
    @Operation(summary = "用户分页列表")
    @PreAuthorize("hasAuthority('user:page')")
    public R<Page<SysUserVO>> page(SysUserPageDTO dto) {
        return R.data(baseService.page(dto));
    }

    @ApiLog
    @GetMapping("list")
    @Operation(summary = "用户列表")
    @PreAuthorize("hasAuthority('user:list')")
    public R<List<SysUserVO>> list(@Validated SysUserListDTO dto) {
        return R.data(baseService.list(dto));
    }

    @ApiLog
    @GetMapping("grant/{id:\\d+}")
    @Operation(summary = "用户权限列表")
    @PreAuthorize("hasAuthority('user:granted-list')")
    public R<SysUserGrantVO> grantedList(@PathVariable("id") Long id) {
        return R.data(baseService.grantedList(id));
    }

    @ApiLog
    @PutMapping("grant/{id:\\d+}")
    @Operation(summary = "修改用户权限列表")
    @PreAuthorize("hasAuthority('user:grant')")
    public IR grant(@PathVariable("id") Long id, @RequestBody SysUserGrantDTO dto) {
        return R.status(baseService.grant(id, dto));
    }

    @ApiLog
    @GetMapping("user-profile")
    @Operation(summary = "当前用户信息")
    @PreAuthorize("isAuthenticated()")
    public R<SysUserProfileVO> userProfile() {
        return R.data(baseService.userProfile());
    }

    @ApiLog
    @PutMapping("user-profile")
    @Operation(summary = "修改用户信息")
    @PreAuthorize("isAuthenticated()")
    public IR modifyUserProfile(@Validated @RequestBody SysUserProfileDTO dto) {
        return R.status(baseService.modifyUserProfile(dto));
    }

    @ApiLog
    @Operation(summary = "修改用户密码")
    @PreAuthorize("isAuthenticated()")
    @PutMapping("reset-password")
    public IR resetPassword(@Validated @RequestBody SysUserRestPasswordDTO dto) {
        return R.status(baseService.resetPassword(dto));
    }

}
